<template>
    <div class="bk">
        <el-card class="rg_layout">
            <div class="rg_left">
                <p>新用户注册</p>
                <p>USER REGISTER</p>
            </div>
            <div class="rg_center">
                <div class="regiseter">
                    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="120px"
                        class="demo-ruleForm" :size="formSize" status-icon>
                        <el-form-item label="账号" prop="username">
                            <el-input v-model="ruleForm.username" />
                        </el-form-item>
                        <el-form-item label="密码" prop="password">
                            <el-input type="password" v-model="ruleForm.password" />
                        </el-form-item>
                        <el-form-item label="再次输入密码" prop="repassword">
                            <el-input type="password" v-model="ruleForm.repassword" />
                        </el-form-item>
                        <el-form-item label="学号" prop="stuid">
                            <el-input v-model="ruleForm.stuid" />
                        </el-form-item>
                        <el-form-item label="姓名" prop="name">
                            <el-input v-model="ruleForm.name" />
                        </el-form-item>
                        <el-form-item label="性别" prop="sex">
                            <el-radio-group v-model="ruleForm.sex">
                                <el-radio label="0">男</el-radio>
                                <el-radio label="1">女</el-radio>
                            </el-radio-group>
                        </el-form-item>
                        <el-form-item label="所属院系" prop="department">
                            <el-input v-model="ruleForm.department" />
                        </el-form-item>
                        <el-form-item label="专业" prop="major">
                            <el-input v-model="ruleForm.major" />
                        </el-form-item>
                        <el-form-item label="邮箱" prop="email">
                            <el-input v-model="ruleForm.email" />
                        </el-form-item>
                        <el-form-item label="手机号码" prop="phonenumber">
                            <el-input v-model="ruleForm.phonenumber" />
                        </el-form-item>
                        <el-form-item label="出生日期" prop="birthday">
                            <el-date-picker v-model="ruleForm.birthday" type="date" label="请选择日期" placeholder="请选择日期"
                                style="width: 100%" />
                        </el-form-item>
                        <el-row>
                            <el-col :span="12"><el-button type="primary" @click="submitForm()">
                                    注册
                                </el-button></el-col>
                            <el-col :span="2"></el-col>
                            <el-col :span="10"><el-button @click="resetForm()">重置</el-button></el-col>
                        </el-row>
                    </el-form>
                    <span>已有账号?<el-link @click= "cancel()" type="primary">点我登录</el-link></span>
                </div>
            </div>
        </el-card>

    </div>
</template>
<script setup>
import { ref } from 'vue';
import required from '@/utils/request'
import router from '@/router'
import { ElMessage } from 'element-plus'

const ruleFormRef = ref(null);
const formSize = ref('default')
const ruleForm = ref({
    username: '',
    password: '',
    repassword: '',
    stuid: '',
    name: '',
    sex: '',
    department: '',
    major: '',
    email: '',
    phonenumber: '',
    birthday: ''
})

const validatePass = (rule, value, callback) => {
    if (value === '') {
        callback(new Error('请输入密码'));
    }
    else {
        if (ruleForm.value.repassword !== ruleForm.value.password) {
            if (ruleForm.value.repassword != '') {
                callback(new Error('两次输入的密码不一致'))
            }
        }
    }
    return true;
}

const validatePass2 = (rule, value, callback) => {
    if (value === '') {
        callback(new Error('请再次输入密码'));
    } else {
        if (ruleForm.value.repassword !== ruleForm.value.password) {
            callback(new Error('两次输入的密码不一致'))
        }
    }
    return true;
}
const rules = ref({
    username: [
        { required: true, message: '请输入账号', trigger: 'blur' },
        { min: 5, message: '长度至少5位', trigger: 'blur' },
    ],
    password: [
        { required: true, validator: validatePass, trigger: 'blur' },
    ],
    repassword: [
        { required: true, validator: validatePass2, trigger: 'blur' },
    ],
    stuid: [
        {
            required: true,
            message: '学号必填',
            trigger: 'blur',
        },
    ],
    name: [
        {
            required: true,
            message: '姓名必填',
            trigger: 'blur',
        },
    ],
    sex: [
        {
            required: true,
            message: '性别必填',
            trigger: 'blur',
        },
    ],
    department: [
        {
            required: true,
            message: '院系必填',
            trigger: 'blur',
        },
    ],
    major: [
        {
            required: true,
            message: '专业必填',
            trigger: 'blur',
        },
    ],
    email: [
        {
            required: true,
            message: '邮箱必填',
            trigger: 'blur',
        },
    ],
    phonenumber: [
        { required: true, message: '手机必填', trigger: 'blur' },
    ],
    birthday: [{
        type: 'date',
        required: true,
        message: '出生日期必填',
        trigger: 'blur'
    }]
})

/**
 * 提交操作
 */
const submitForm = async () => {
    const result = await required.post('/user/register', ruleForm.value);
    if (result.data.code === 200) {
        router.replace('/login');
    } else {
        ElMessage.error(result.data.msg);
    }
}

/**
 * 重置操作
 */
const resetForm = () => {
    ruleForm.value.username = '';
    ruleForm.value.password = '';
    ruleForm.value.repassword = '';
    ruleForm.value.stuid = '';
    ruleForm.value.name = '';
    ruleForm.value.sex = '';
    ruleForm.value.department = '';
    ruleForm.value.major = '';
    ruleForm.value.email = '';
    ruleForm.value.phonenumber = '';
    ruleForm.value.birthday = '';
}

/**
 * 点我登录链接
 */
const cancel = () => {
    router.replace('/login');
}
</script>
<style>
* {
    margin: 0px;
    padding: 0px;
    box-sizing: border-box;
}

.rg_layout {
    width: 800px;
    height: 700px;
    border: 8px solid #EEEEEE;
    background-color: white;
    /*让div水平居中*/
    margin: auto;
}

.rg_left {
    /*border: 1px solid red;*/
    float: left;
    margin: 15px;
}

.rg_left>p:first-child {
    color: #FFD026;
    font-size: 20px;
}

.rg_left>p:last-child {
    color: #A6A6A6;
    font-size: 20px;

}


.rg_center {
    float: left;

}

.td_left {
    width: 100px;
    text-align: right;
    height: 45px;
    color: #A6A6A6;
}

.td_right {
    padding-left: 50px;
    color: #A6A6A6;
}

.el-button {
    width: 200px;
}

#username,
#password,
#email,
#name,
#tel,
#birthday,
#checkcode {
    width: 251px;
    height: 32px;
    border: 1px solid #A6A6A6;
    /*设置边框圆角*/
    border-radius: 5px;
    padding-left: 10px;
}

#checkcode {
    width: 110px;
}

#img_check {
    height: 32px;
    vertical-align: middle;
}

#btn_sub {
    width: 150px;
    height: 40px;
    background-color: #FFD026;
    border: 1px solid #FFD026;
}

.regiseter {
    width: 400px;
}

.title {
    font-family: Helvetica, 'Hiragino Sans GB', 'Microsoft Yahei', '微软雅黑', Arial, sans-serif;
    color: blue;
    font-size: x-large;
    text-align: center;
}

.pinyin {
    font-family: Helvetica, 'Hiragino Sans GB', 'Microsoft Yahei', '微软雅黑', Arial, sans-serif;
    color: blue;
    font-size: x-large;
    text-align: center;
}

.bk {
    width: 100%;
    height: 100%;
    padding-left: 50%;
    padding-top: 100px;
    background-image: url('../assets/images/lgbk1.jpg');
}
</style>